package com.qdsg.ysg.doctor.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * Description
 * Created by lbj on 2022/4/8 10:37
 */

public class CancelOrderDialog extends BaseDialogFragment {


    private Dialog mDialog;
    private OnItemClickListener mListener;
    private EditText edt_detail;
    private String id, fromWhere;


    public static CancelOrderDialog newInstance(String id, String fromWhere) {
        Bundle args = new Bundle();
        args.putString( "id", id );
        args.putString( "fromWhere", fromWhere );

        CancelOrderDialog fragment = new CancelOrderDialog();
        fragment.setArguments( args );
        return fragment;
    }


    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_cancel_order, container );
        assert getArguments() != null;
        id = getArguments().getString( "id" );
        fromWhere = getArguments().getString( "fromWhere" );
        TextView tv_cancel = view.findViewById( R.id.tv_cancel );
        TextView tvType = view.findViewById( R.id.tv_type );
        EditText et_cancel_reason = view.findViewById( R.id.et_cancel_reason );
        TextView tv_sure = view.findViewById( R.id.tv_sure );
        if ("1".equals( fromWhere )) {
            tvType.setText( "取消订单" );
            et_cancel_reason.setHint( "请填写取消订单原因" );
        } else {
            tvType.setText( "护理小结" );
            et_cancel_reason.setHint( "请填写护理小结" );
        }
        tv_cancel.setOnClickListener( v -> {
            if (mListener != null) {
//                mListener.onCancel();
                dismiss();
            }
        } );
        tv_sure.setOnClickListener( v -> {
            if ("1".equals( fromWhere )) {
                if (UtilString.isEmpty( et_cancel_reason.getText().toString().trim() )) {
                    ToastUtil.showToastCenter( mContext, "取消订单原因不能为空" );
                    return;
                }
                cancelForDoc( et_cancel_reason.getText().toString().trim() );
            } else {
                if (UtilString.isEmpty( et_cancel_reason.getText().toString().trim() )) {
                    ToastUtil.showToastCenter( mContext, "护理小结不能为空" );
                    return;
                }
                mListener.onConfirm(et_cancel_reason.getText().toString().trim());
                dismiss();
            }


        } );

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.BOTTOM );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String conclusion);

    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    private void cancelForDoc(String cancelReason) {
        showProgressDialog( mContext );
        Map<String, Object> map = new HashMap<>();
        map.put( "id", id );
        map.put( "cancelReason", cancelReason );
        RestProxy.getInstance().cancelForDoc( map, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                mListener.onConfirm("");
                dismiss();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                dismissProgressDialog();
            }

            @Override
            public void onComplete() {
                dismissProgressDialog();
            }
        } );


    }
}
